// pages/collection/collection.js
var utils = require('../../utils/utils.js');
var request = require('../../utils/request.js');
var auth = require('../../utils/auth.js');
var config = require('../../config.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: '',
    id: '',
    name: '',
    qrcodeImg: '/images/com/code.png',
    disabled: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new app.WeToast();
    
    var money = options.money;
    var id = options.id;
    var name = options.name;
    this.setData({
      money: money,
      id: id
    })

    this.getQrcodeImg({
      money: money,
      id: id,
      name: name
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  // clearTap(e){
  //   this.setData({
  //     money: ''
  //   })
  // },
  getQrcodeImg(data){
    var that = this;
    request.get({'path': config.createOrder}, data).then(function(res){
      var ratingData = res.data;
      if (ratingData.ret == 0) {
        
        that.setData({
          qrcodeImg: ratingData.data.imgUrl
        })

      } else {
        wx.showToast({
          icon: 'none',
          title: ratingData.msg
        })
        return false;
      }
    }, function(res){
      wx.showToast({
        icon: 'none',
        title: res.data.msg
      })
      return false;
    })
  },
  scanTap(e) {
    var that = this;
    wx.scanCode({
      success: function(res) {
        console.log(res)
        var data = {
          id: that.data.id,
          code: res.result,
          name: that.data.name,
          money: that.data.money
        }
        request.get({'path': config.createOrder}, data).then(function(res){
          var ratingData = res.data;
          if (ratingData.ret == 0) {
            wx.navigateTo({
              url: `/pages/list/list/list?id=${id}`
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: ratingData.msg
            })
            return false;
          }
        }, function(res){
          wx.showToast({
            icon: 'none',
            title: res.data.msg
          })
          return false;
        })
      },
      fail: function(res) {
        console.log(res)
      }
    })
  }
})